<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="和风预警id" prop="warningCode">
        <el-input
          v-model="queryParams.warningCode"
          placeholder="请输入和风预警id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警标题" prop="warningTitle">
        <el-input
          v-model="queryParams.warningTitle"
          placeholder="请输入预警标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="warningStarttime">
        <el-date-picker clearable
          v-model="queryParams.warningStarttime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="warningEndtime">
        <el-date-picker clearable
          v-model="queryParams.warningEndtime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发布状态" prop="warningStatus">
        <el-select v-model="queryParams.warningStatus" placeholder="请选择发布状态" clearable>
          <el-option
            v-for="dict in grape_warning_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警等级" prop="warningSeverity">
        <el-input
          v-model="queryParams.warningSeverity"
          placeholder="请输入预警等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警类型" prop="warningType">
        <el-input
          v-model="queryParams.warningType"
          placeholder="请输入预警类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警类型名称" prop="warningTypename">
        <el-input
          v-model="queryParams.warningTypename"
          placeholder="请输入预警类型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细描述" prop="warningText">
        <el-input
          v-model="queryParams.warningText"
          placeholder="请输入详细描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['grape:warning:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grape:warning:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grape:warning:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['grape:warning:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预警编号" align="center" prop="warningId" />
      <el-table-column label="和风预警id" align="center" prop="warningCode" />
      <el-table-column label="预警标题" align="center" prop="warningTitle" />
      <el-table-column label="开始时间" align="center" prop="warningStarttime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warningStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="warningEndtime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warningEndtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布状态" align="center" prop="warningStatus">
        <template #default="scope">
          <dict-tag :options="grape_warning_status" :value="scope.row.warningStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="预警等级" align="center" prop="warningSeverity" />
      <el-table-column label="预警类型" align="center" prop="warningType" />
      <el-table-column label="预警类型名称" align="center" prop="warningTypename" />
      <el-table-column label="详细描述" align="center" prop="warningText" />
      <el-table-column label="备注" align="center" prop="warningNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['grape:warning:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grape:warning:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警历史信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="warningRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="和风预警id" prop="warningCode">
          <el-input v-model="form.warningCode" placeholder="请输入和风预警id" />
        </el-form-item>
        <el-form-item label="预警标题" prop="warningTitle">
          <el-input v-model="form.warningTitle" placeholder="请输入预警标题" />
        </el-form-item>
        <el-form-item label="开始时间" prop="warningStarttime">
          <el-date-picker clearable
            v-model="form.warningStarttime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="warningEndtime">
          <el-date-picker clearable
            v-model="form.warningEndtime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布状态" prop="warningStatus">
          <el-select v-model="form.warningStatus" placeholder="请选择发布状态">
            <el-option
              v-for="dict in grape_warning_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预警等级" prop="warningSeverity">
          <el-input v-model="form.warningSeverity" placeholder="请输入预警等级" />
        </el-form-item>
        <el-form-item label="预警类型" prop="warningType">
          <el-input v-model="form.warningType" placeholder="请输入预警类型" />
        </el-form-item>
        <el-form-item label="预警类型名称" prop="warningTypename">
          <el-input v-model="form.warningTypename" placeholder="请输入预警类型名称" />
        </el-form-item>
        <el-form-item label="详细描述" prop="warningText">
          <el-input v-model="form.warningText" placeholder="请输入详细描述" />
        </el-form-item>
        <el-form-item label="备注" prop="warningNote">
          <el-input v-model="form.warningNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Warning">
import { listWarning, getWarning, delWarning, addWarning, updateWarning } from "@/api/grape/warning";

const { proxy } = getCurrentInstance();
const { grape_warning_status } = proxy.useDict('grape_warning_status');

const warningList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    warningCode: null,
    warningTitle: null,
    warningStarttime: null,
    warningEndtime: null,
    warningStatus: null,
    warningSeverity: null,
    warningType: null,
    warningTypename: null,
    warningText: null,
    warningNote: null
  },
  rules: {
    warningCode: [
      { required: true, message: "和风预警id不能为空", trigger: "blur" }
    ],
    warningTitle: [
      { required: true, message: "预警标题不能为空", trigger: "blur" }
    ],
    warningStatus: [
      { required: true, message: "发布状态不能为空", trigger: "change" }
    ],
    warningSeverity: [
      { required: true, message: "预警等级不能为空", trigger: "blur" }
    ],
    warningType: [
      { required: true, message: "预警类型不能为空", trigger: "blur" }
    ],
    warningTypename: [
      { required: true, message: "预警类型名称不能为空", trigger: "blur" }
    ],
    warningText: [
      { required: true, message: "详细描述不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询预警历史信息列表 */
function getList() {
  loading.value = true;
  listWarning(queryParams.value).then(response => {
    warningList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    warningId: null,
    warningCode: null,
    warningTitle: null,
    warningStarttime: null,
    warningEndtime: null,
    warningStatus: null,
    warningSeverity: null,
    warningType: null,
    warningTypename: null,
    warningText: null,
    warningNote: null
  };
  proxy.resetForm("warningRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.warningId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警历史信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _warningId = row.warningId || ids.value
  getWarning(_warningId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预警历史信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warningRef"].validate(valid => {
    if (valid) {
      if (form.value.warningId != null) {
        updateWarning(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarning(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _warningIds = row.warningId || ids.value;
  proxy.$modal.confirm('是否确认删除预警历史信息编号为"' + _warningIds + '"的数据项？').then(function() {
    return delWarning(_warningIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('grape/warning/export', {
    ...queryParams.value
  }, `warning_${new Date().getTime()}.xlsx`)
}

getList();
</script>
